﻿@namespace Oqtane.Modules.Admin.Upgrade
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IFileService FileService
@inject IPackageService PackageService
@inject IInstallationService InstallationService

@if (_package != null)
{
    <TabStrip>
        <TabPanel Name="Download">
            @if (_upgradeavailable)
            {
                <ModuleMessage Type="MessageType.Info" Message="Select The Upgrade Button To Install a New Framework Version"></ModuleMessage>
                <button type="button" class="btn btn-success" @onclick=@(async () => await Download(Constants.PackageId, @_package.Version))>Upgrade To @_package.Version</button>
            }
            else
            {
                <ModuleMessage Type="MessageType.Info" Message="Framework Is Already Up To Date"></ModuleMessage>
            }
        </TabPanel>
        <TabPanel Name="Upload">
            <table class="table table-borderless">
                <tr>
                    <td>
                        <Label HelpText="Upload a framework package and select Install to complete the installation">Framework: </Label>
                    </td>
                    <td>
                        <FileManager Filter="nupkg" ShowFiles="false" Folder="Framework" />
                    </td>
                </tr>
            </table>
            <button type="button" class="btn btn-success" @onclick="Upgrade">Install</button>
        </TabPanel>
    </TabStrip>
}

@code {
    private Package _package;
    private bool _upgradeavailable = false;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            List<Package> packages = await PackageService.GetPackagesAsync("framework");
            if (packages != null)
            {
                _package = packages.FirstOrDefault();
                if (_package != null)
                {
                    _upgradeavailable = (Version.Parse(_package.Version).CompareTo(Version.Parse(Constants.Version)) > 0);
                }
                else
                {
                    _package = new Package { Name = Constants.PackageId, Version = Constants.Version };
                }
            }
        }
        catch
        {
            // can be caused by no network connection
        }
    }

    private async Task Upgrade()
    {
        try
        {
            ShowProgressIndicator();
            var interop = new Interop(JSRuntime);
            await interop.RedirectBrowser(NavigateUrl(), 10);
            await InstallationService.Upgrade();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Executing Upgrade {Error}", ex.Message);
            AddModuleMessage("Error Executing Upgrade", MessageType.Error);
        }
    }

    private async Task Download(string packageid, string version)
    {
        try
        {
            await PackageService.DownloadPackageAsync(packageid, version, "Framework");
            ShowProgressIndicator();
            var interop = new Interop(JSRuntime);
            await interop.RedirectBrowser(NavigateUrl(), 10);
            await InstallationService.Upgrade();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Downloading Framework {Error}", ex.Message);
            AddModuleMessage("Error Downloading Framework", MessageType.Error);
        }
    }
}
